package com.example.leetdemo.finash;

import java.util.Stack;

public class Solution71 {

    public String simplifyPath(String path) {
        String[] pathStr = path.split("/");
        Stack<String> stringStack = new Stack<>();
        for (int i = 0; i < pathStr.length; i++) {
            if (pathStr[i] != null && !pathStr[i].equals("")) {
                if (pathStr[i].equals("..")) {
                    if (stringStack.size() > 0) {
                        stringStack.pop();
                    }
                    continue;
                }
                if (pathStr[i].equals(".")) {
                    continue;
                }
                stringStack.add(pathStr[i]);
            }
        }
        String result = "";
        while (stringStack.size() > 0) {
            result = "/" + stringStack.pop() + result;
        }
        if (result.equals("")) {
            result = "/";
        }
        return result;
    }

    public static void main(String[] args) {
        System.err.println("1/2//33/4".split("/").length);
        Solution71 solution71 = new Solution71();
        System.err.println(solution71.simplifyPath("1/2//../33/4"));
    }
}
